package cn.chenc.framework.excel.converters.localdatetime;


import cn.chenc.framework.core.util.DateUtils;
import com.alibaba.excel.annotation.format.DateTimeFormat;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import lombok.Data;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Objects;

/**
 * 　@description: 自定义转化器，将String转化为LocalDateTime
 * 　@author 陈_C
 * 　@date 2020/8/26 16:32
 *
 */
@Data
public class LocalDateTimeStringConverter implements Converter<LocalDateTime> {

    /**
     * 不使用{@code @LocalDateTimeFormat}注解指定日期格式时,默认会使用该格式.
     */
    private static final String PATTERN = "yyyy-MM-dd HH:mm:ss";


    private String defaultPattern;

    private String readPattern;

    public LocalDateTimeStringConverter() {
        this.defaultPattern = PATTERN;
        this.readPattern = PATTERN;
    }

    public LocalDateTimeStringConverter(String defaultPattern, String readPattern) {
        this.defaultPattern = defaultPattern;
        this.readPattern = readPattern;
    }

    @Override
    public Class supportJavaTypeKey() {
        return LocalDateTime.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    /**
     * 这里读的时候会调用
     *
     * @param cellData            excel数据 (NotNull)
     * @param contentProperty     excel属性 (Nullable)
     * @param globalConfiguration 全局配置 (NotNull)
     * @return 读取到内存中的数据
     */
    @Override
    public LocalDateTime convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
        DateTimeFormat annotation = contentProperty.getField().getAnnotation(DateTimeFormat.class);
//        LocalDateTime localDateTime = LocalDateTime.parse(cellData.getStringValue(), DateTimeFormatter.ofPattern(Objects.nonNull(annotation) ? annotation.value() : this.readPattern));
        Date date = DateUtils.parse(cellData.getStringValue());
        LocalDateTime localDateTime = DateUtils.dateToLocalDateTime(date);
//        LocalDateTime localDateTime = localDate.atStartOfDay();
//        return LocalDateTime.parse(cellData.getStringValue(),
//                DateTimeFormatter.ofPattern(Objects.nonNull(annotation) ? annotation.value() : DEFAULT_PATTERN));
        return localDateTime;
    }

    /**
     * 写的时候会调用
     *
     * @param value               java value (NotNull)
     * @param contentProperty     excel属性 (Nullable)
     * @param globalConfiguration 全局配置 (NotNull)
     * @return 写出到excel文件的数据
     */
    @Override
    public WriteCellData convertToExcelData(LocalDateTime value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
        DateTimeFormat annotation = contentProperty.getField().getAnnotation(DateTimeFormat.class);
        return new WriteCellData(value.format(DateTimeFormatter.ofPattern(Objects.nonNull(annotation) ? annotation.value() : this.defaultPattern)));
    }

}
